﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using CSharpDesign.DAL;

namespace CSharpDesign
{
    public partial class SearchSP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataListSource();
                BindDropdownListSource();
            }
        }

        /// <summary>
        /// 绑定列表数据
        /// </summary>
        private void BindDataListSource()
        {
            //绑定列表数据
            int CurPage;
            PagedDataSource objPds = new PagedDataSource();
            objPds.DataSource = SearchSPBLL.GetCommodityList().Tables[0].DefaultView;
            objPds.AllowPaging = true;
            objPds.PageSize = 16;

            if (Request.QueryString["Page"] != null)
                CurPage = Convert.ToInt32(Request.QueryString["Page"]);
            else
                CurPage = 1;

            objPds.CurrentPageIndex = CurPage - 1;
            lblCurrentPage.Text = CurPage.ToString();
            lblSumPage.Text = objPds.PageCount.ToString();

            if (!objPds.IsFirstPage)
            {
                hyfirst.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + 1;
                lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
            }
            if (!objPds.IsLastPage)
            {
                hylastpage.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + objPds.PageCount;
                lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
            }

            DataList1.DataSource = objPds;
            DataList1.DataBind();
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("SearchShangPinList.aspx?SPName=" + TxtName.Text + "&ShangPinFenLeiName=" + DropDownList1.SelectedItem.Text);
        }

        /// <summary>
        /// 绑定下拉列表数据
        /// </summary>
        private void BindDropdownListSource()
        {
            DropDownList1.DataSource = new SearchSPBLL().GetCommodityCategory();
            DropDownList1.DataValueField = "id";
            DropDownList1.DataTextField = "name";
            DropDownList1.DataBind();
            DropDownList1.Items.Add("所有类别");
            DropDownList1.Items[this.DropDownList1.Items.Count - 1].Value = " ";
            DropDownList1.SelectedIndex = this.DropDownList1.Items.Count - 1;
        }
    }
}